<template>
  <!-- eslint-disable vue/html-indent -->
  <!-- eslint-disable vue/html-self-closing -->
  <!-- eslint-disable vue/html-closing-bracket-newline -->
  <!-- eslint-disable vue/max-attributes-per-line -->
  <div>
    <el-table style="width: 100%" border :data="records">
      <el-table-column
        label="序号"
        type="index"
        width="80"
        align="center"
      ></el-table-column>
      <el-table-column prop="skuName" label="名称"></el-table-column>
      <el-table-column prop="skuDesc" label="描述"></el-table-column>
      <el-table-column align="center" width="110" label="默认图片">
        <template slot-scope="{ row }">
          <img :src="row.skuDefaultImg" style="width: 60px; height: 60px" />
        </template>
      </el-table-column>
      <el-table-column
        prop="weight"
        width="80"
        label="重量(KG)"
      ></el-table-column>
      <el-table-column
        prop="price"
        width="80"
        label="价格(元)"
      ></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{ row }">
          <el-button
            v-if="row.isSale == 0"
            type="info"
            icon="el-icon-sort-down"
            size="mini"
            @click="sale(row)"
          ></el-button>
          <el-button
            v-else
            type="success"
            icon="el-icon-sort-up"
            size="mini"
            @click="cancel(row)"
          ></el-button>
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="edit"
          ></el-button>
          <el-button
            type="info"
            icon="el-icon-info"
            size="mini"
            @click="getSkuInfo(row)"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="text-align: center; margin-top: 15px"
      :current-page="page"
      :page-sizes="[3, 5, 10]"
      :page-size="limit"
      layout="prev, pager, next, jumper, ->, sizes, total"
      :total="total"
      @current-change="getSkuList"
      @size-change="handleSizeChange"
    >
      <!--  @size-change="handleSizeChange"
      @current-change="handleCurrentChange" -->
    </el-pagination>
    <el-drawer size="50%" :visible.sync="drawer" :show-close="false">
      <el-row>
        <el-col :span="5" class="col-font">名称</el-col>
        <el-col :span="16">{{ skuInfo.skuName }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5" class="col-font">描述</el-col>
        <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5" class="col-font">价格</el-col>
        <el-col :span="16">{{ skuInfo.price }}元</el-col>
      </el-row>
      <el-row>
        <el-col :span="5" class="col-font">平台属性</el-col>
        <el-col :span="16">
          <template>
            <el-tag
              v-for="attr in skuInfo.skuAttrValueList"
              :key="attr.id"
              style="margin-right: 10px"
              type="success"
              >{{ attr.attrId }}-{{ attr.valueId }}</el-tag
            >
          </template>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5" class="col-font">商品图片</el-col>
        <el-col :span="16">
          <el-carousel class="carousel">
            <el-carousel-item
              v-for="item in skuInfo.skuImageList"
              :key="item.id"
            >
              <img :src="item.imgUrl" />
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
/* eslint-disable eqeqeq */
/* eslint-disable comma-dangle */
/* eslint-disable quotes */
/* eslint-disable semi */
/* eslint-disable spaced-comment */
export default {
  name: "Sku",
  data() {
    return {
      page: 1,
      limit: 5,
      records: [], //存储sku列表的数据
      total: 0,
      skuInfo: {}, //存储SKU信息
      drawer: false,
    };
  },
  mounted() {
    //获取sku列表
    this.getSkuList();
  },
  methods: {
    //获取sku列表
    async getSkuList(pages = 1) {
      this.page = pages;
      await this.$API.sku.reqSkuList(this.page, this.limit).then((result) => {
        if (result.code == 200) {
          this.total = result.data.total;
          this.records = result.data.records;
        }
      });
    },
    handleSizeChange(limit) {
      this.limit = limit;
      this.getSkuList();
    },
    //上架
    async sale(row) {
      await this.$API.sku.reqSale(row.id).then((result) => {
        if (result.code == 200) {
          this.$message({
            type: "success",
            message: "上架成功",
          });
          row.isSale = 1;
        }
      });
    },
    //下架
    async cancel(row) {
      await this.$API.sku.reqCancel(row.id).then((result) => {
        if (result.code == 200) {
          row.isSale = 0;
          this.$message({
            type: "success",
            message: "下架成功",
          });
        }
      });
    },
    edit() {
      this.$message({
        message: "正在开发中...",
      });
    },
    //获取SKU详情的方法
    async getSkuInfo(row) {
      this.drawer = true;
      await this.$API.sku.reqSkuById(row.id).then((result) => {
        if (result.code == 200) {
          this.skuInfo = result.data;
        }
      });
    },
  },
};
</script>

<style>
.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
.el-carousel__button {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background-color: lightgray;
}
</style>
<style scoped>
.col-font {
  font-size: 18px;
  text-align: right;
}
.el-col {
  margin: 10px;
}
.carousel {
  width: 300px;
  height: 300px;
}
.carousel img {
  width: 100%;
  height: 100%;
}
</style>
